package com.cskaoyan.service;

import com.cskaoyan.bean.bo.back.UpdateGoodsBO;
import com.cskaoyan.bean.po.*;
import com.cskaoyan.bean.vo.back.ShowCatAndBrandByIdVO;
import com.cskaoyan.bean.vo.back.ShowCatAndBrandVO;
import com.cskaoyan.mapper.*;
import com.cskaoyan.bean.bo.goodsbo.GoodsBo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @program: SSMItem
 * @description 该service处理商品管理模块的数据
 * @author: YY
 * @create: 2022-10-12 21:09
 **/

@Service
public class AdminGoodsServiceImpl implements AdminGoodsService {

    @Autowired
    MarketGoodsMapper mapper;

    @Autowired
    MarketCategoryMapper marketCategoryMapper;

    @Autowired
    MarketBrandMapper marketBrandMapper;

    @Autowired
    MarketGoodsAttributeMapper attributeMapper;

    @Autowired
    MarketGoodsSpecificationMapper specificationMapper;

    @Autowired
    MarketGoodsProductMapper productMapper;

    @Autowired
    MarketGoodsProductMapper ProductMapper;

    @Autowired
    MarketGoodsSpecificationMapper SpecificationMapper;


    @Override

    public List<MarketGoods> selectGoods(BaseParam baseParam, Integer goodsSn, String name, Integer goodsId) {

        MarketGoodsExample example = new MarketGoodsExample();


        example.setDistinct(true);

        MarketGoodsExample.Criteria criteria = example.createCriteria();

        example.setOrderByClause(baseParam.getSort() + " " + baseParam.getOrder());
        criteria.andDeletedEqualTo(false);
        //首先判断传入查询的值是否为null
        if (StringUtils.isEmpty(goodsId) && StringUtils.isEmpty(name) && StringUtils.isEmpty(goodsSn)) {
            //说明查询全部数据
            List<MarketGoods> marketGoods = mapper.selectByExample(example);
            return marketGoods;
        }
        if (baseParam.getLimit() == 0) {
            List<MarketGoods> marketGoods = mapper.selectByExample(example);
            return marketGoods;
        }

        //说明是模糊查询
        //如果goodsId不为空，优先通过goodsId查询
        if (!StringUtils.isEmpty(goodsId)) {
            criteria.andIdEqualTo(goodsId);

        }
        if (!StringUtils.isEmpty(name)) {
            criteria.andNameLike("%" + name.trim() + "%");
        }
        if (!StringUtils.isEmpty(goodsSn)) {
            criteria.andGoodsSnEqualTo(goodsSn.toString());
        }

        List<MarketGoods> marketGoods = mapper.selectByExample(example);
        return marketGoods;


    }

    @Override
    public void insertGoods(GoodsBo goods) {


        MarketGoods goods1 = goods.getGoods();
        goods1.setAddTime(new Date());
        goods1.setUpdateTime(new Date());
        mapper.insertSelective(goods1);
        //拿到goodsId
        Integer goodsId = goods1.getId();


        //获取specifications
        List<MarketGoodsSpecification> specifications = goods.getSpecifications();

        for (MarketGoodsSpecification specification : specifications) {
            if (specification != null) {
                specification.setAddTime(new Date());
                specification.setUpdateTime(new Date());
                specification.setGoodsId(goodsId);
                SpecificationMapper.insertSelective(specification);
            }
        }

        List<MarketGoodsProduct> products = goods.getProducts();

        for (MarketGoodsProduct product : products) {
            product.setAddTime(new Date());
            product.setUpdateTime(new Date());
            if (product != null) {
                product.setGoodsId(goodsId);
                ProductMapper.insertSelective(product);

            }
        }

        List<MarketGoodsAttribute> attributes = goods.getAttributes();

        for (MarketGoodsAttribute attribute : attributes) {
            if (attribute != null) {
                attribute.setAddTime(new Date());
                attribute.setUpdateTime(new Date());
                attribute.setGoodsId(goodsId);
                attributeMapper.insertSelective(attribute);

            }
        }


    }


    @Override
    public List<MarketGoods> selectAllGoods(Integer limit) {
        List<MarketGoods> list = mapper.selectGoods();

        return list;
    }


    /**
     * 展示将被修改的商品信息
     * @return
     */
    @Override
    public ShowCatAndBrandVO showCatAndBrand() {
        MarketCategoryExample example = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        List<MarketCategory> marketCategories = marketCategoryMapper.selectByExample(example);
        ShowCatAndBrandVO s = new ShowCatAndBrandVO();
        List<ShowCatAndBrandVO.CategoryListEntity> categoryListEntities = new ArrayList<>();
        for (MarketCategory m : marketCategories) {
            if (m.getPid() == 0){
                ShowCatAndBrandVO.CategoryListEntity c = new ShowCatAndBrandVO.CategoryListEntity();
                c.setValue(m.getId());
                c.setLabel(m.getName());
                categoryListEntities.add(c);
            }
        }

        for (ShowCatAndBrandVO.CategoryListEntity c : categoryListEntities) {
            List<ShowCatAndBrandVO.CategoryListEntity.ChildrenEntity> childrenEntities = new ArrayList<>();
            for (MarketCategory m : marketCategories) {
                if (c.getValue() == m.getPid()){
                    ShowCatAndBrandVO.CategoryListEntity.ChildrenEntity childrenEntity = new ShowCatAndBrandVO.CategoryListEntity.ChildrenEntity();
                    childrenEntity.setValue(m.getId());
                    childrenEntity.setLabel(m.getName());
                    childrenEntities.add(childrenEntity);
                }
            }
            c.setChildren(childrenEntities);
        }

        MarketBrandExample brandExample = new MarketBrandExample();
        MarketBrandExample.Criteria exampleCriteria = brandExample.createCriteria();
        exampleCriteria.andDeletedEqualTo(false);
        List<MarketBrand> marketBrands = marketBrandMapper.selectByExample(brandExample);
        List<ShowCatAndBrandVO.BrandListEntity> brandListEntities = new ArrayList<>();
        for (MarketBrand m : marketBrands) {
            ShowCatAndBrandVO.BrandListEntity brandListEntity = new ShowCatAndBrandVO.BrandListEntity();
            brandListEntity.setValue(m.getId());
            brandListEntity.setLabel(m.getName());
            brandListEntities.add(brandListEntity);
        }

        s.setCategoryList(categoryListEntities);
        s.setBrandList(brandListEntities);
        return s;
    }

    /**
     * 回显将被修改的商品信息
     * @return
     */
    @Override
    public ShowCatAndBrandByIdVO showCatAndBrandById(Integer id) {
        MarketGoods marketGoods = mapper.selectByPrimaryKey(id);

        List<Integer> ids = marketCategoryMapper.getIds(marketGoods.getCategoryId());

        MarketGoodsAttributeExample example = new MarketGoodsAttributeExample();
        MarketGoodsAttributeExample.Criteria criteria = example.createCriteria();
        criteria.andGoodsIdEqualTo(marketGoods.getId());
        List<MarketGoodsAttribute> marketGoodsAttributes = attributeMapper.selectByExample(example);

        MarketGoodsSpecificationExample specificationExample = new MarketGoodsSpecificationExample();
        MarketGoodsSpecificationExample.Criteria exampleCriteria = specificationExample.createCriteria();
        exampleCriteria.andGoodsIdEqualTo(marketGoods.getId());
        List<MarketGoodsSpecification> marketGoodsSpecifications = specificationMapper.selectByExample(specificationExample);

        MarketGoodsProductExample productExample = new MarketGoodsProductExample();
        MarketGoodsProductExample.Criteria productExampleCriteria = productExample.createCriteria();
        productExampleCriteria.andGoodsIdEqualTo(marketGoods.getId());
        List<MarketGoodsProduct> marketGoodsProducts = productMapper.selectByExample(productExample);

        ShowCatAndBrandByIdVO showCatAndBrandByIdVO = new ShowCatAndBrandByIdVO();
        showCatAndBrandByIdVO.setCategoryIds(ids);
        showCatAndBrandByIdVO.setGoods(marketGoods);
        showCatAndBrandByIdVO.setAttributes(marketGoodsAttributes);
        showCatAndBrandByIdVO.setSpecifications(marketGoodsSpecifications);
        showCatAndBrandByIdVO.setProducts(marketGoodsProducts);
        return showCatAndBrandByIdVO;
    }

    /**
     * 修改商品信息
     * @param updateGoodsBO
     * @return
     */
    @Override
    public int updateGoods(UpdateGoodsBO updateGoodsBO) {
        MarketGoodsExample goodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria goodsExampleCriteria = goodsExample.createCriteria();
        goodsExampleCriteria.andIdEqualTo(updateGoodsBO.getGoods().getId());
        mapper.updateByExampleSelective(updateGoodsBO.getGoods(), goodsExample);

        List<MarketGoodsAttribute> attributes = updateGoodsBO.getAttributes();
        for (MarketGoodsAttribute attribute : attributes) {
            MarketGoodsAttributeExample attributeExample = new MarketGoodsAttributeExample();
            MarketGoodsAttributeExample.Criteria attributeExampleCriteria = attributeExample.createCriteria();
            attributeExampleCriteria.andIdEqualTo(attribute.getId());
            attributeMapper.updateByExampleSelective(attribute, attributeExample);
        }

        List<MarketGoodsSpecification> specifications = updateGoodsBO.getSpecifications();
        for (MarketGoodsSpecification specification : specifications) {
            MarketGoodsSpecificationExample specificationExample = new MarketGoodsSpecificationExample();
            MarketGoodsSpecificationExample.Criteria specificationExampleCriteria = specificationExample.createCriteria();
            specificationExampleCriteria.andIdEqualTo(specification.getId());
            specificationMapper.updateByExampleSelective(specification, specificationExample);
        }

        List<MarketGoodsProduct> products = updateGoodsBO.getProducts();
        for (MarketGoodsProduct product : products) {
            MarketGoodsProductExample productExample = new MarketGoodsProductExample();
            MarketGoodsProductExample.Criteria productExampleCriteria = productExample.createCriteria();
            productExampleCriteria.andIdEqualTo(product.getId());
            productMapper.updateByExampleSelective(product, productExample);
        }

        return 200;
    }

    /**
     * 删除商品
     * @param marketGoods
     * @return
     */
    @Override
    public int deleteGoods(MarketGoods marketGoods) {
        MarketGoodsExample example = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(marketGoods.getId());
        marketGoods.setDeleted(true);
        mapper.updateByExampleSelective(marketGoods, example);
        return 200;
    }
}

